//var app = angular.module('blogApp', ['utils', 'hrApp']);

app.controller('blogCtrl', function($scope, $http,$location, $anchorScroll, backendSrv, auth){

    $scope.user = auth.getUser();

    $scope.date = new Date();
    $scope.months = ['January', 'Febraury', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

    $scope.toHtml = function(){
       $scope.md = marked($scope.post.body); 
    }

    $scope.savePost = function(){
        backendSrv.cmd('blog/cmd', {Cmd:'addPost', Data: $scope.post}, function(res){
            $scope.post._id = res.data._id;
            //$scope.res = res;
            $scope.listPosts();
        });
    }
    $scope.deleteAllPosts = function(){
        backendSrv.cmd('blog/cmd', {Cmd:'deleteAllPosts', Data: $scope.post}, function(res){
            resetPage();
            $scope.res = res;
            $scope.posts = null;
        });
    }
    $scope.deletePost = function(id){
        console.log('deleting id: ' + id);

        id = (id) ? id : $scope.post._id;
        backendSrv.cmd('blog/cmd', {Cmd:'deletePost', Data: {_id:id} }, function(res){
            resetPage();
            // list of post titles is returned after single deletion
            $scope.posts = res.data;
        });
    }

    /*get post by id*/
    $scope.getPost = function(id){
        backendSrv.cmd('blog/cmd', {Cmd:'getPost', Data: {id:id}}, function(res){
            resetPage();
            $scope.post = res.data;
            $scope.toHtml();
        });
    }
    $scope.listPosts = function(){
        backendSrv.cmd('blog/cmd', {Cmd:'listPosts'}, function(res){
            $scope.posts = res.data;
        });
    }
    $scope.listHomePosts = function(){
        backendSrv.cmd('blog/cmd', {Cmd:'listHomePosts'}, function(res){
            var posts = res.data;
            for(i = 0; i<res.data.length; i++){
                var post = posts[i],
                    sections = post.body.split('<section>');

                post.shortHtml = marked(post.body.split('<section>')[0]);
                post.fullHtml = sections.length > 1 ? marked(sections[1]):null;

                post.full = false;
            }//for

            $scope.homePosts = posts;
            $scope.full = true;
        });
    }
    $scope.saveAsNew = function(){
        delete $scope.post._id;
        $scope.savePost();
    }
    $scope.addComment = function(id){
        var post = findPost(id),
            user = auth.getUser();

        backendSrv.cmd('blog/cmd', {Cmd:'addComment', Data:{_id:post._id, comment:{text: post.comment, email:user.email, name:user.name}} }, function(res){
            post.comments = res.data.comments;
            post.comment = null;

        });
    }
    $scope.deleteComment = function(postId, commentIndex){
        var post = findPost(postId);
        backendSrv.cmd('blog/cmd', {Cmd:'deleteComment', Data:{_id:postId, commentIndex: commentIndex} }, function(res){
            console.log(res.data);
            post.comments = res.data.comments;
        });
    }

    var findPost = $scope.findPost = function(id){
        return _.find($scope.homePosts, function(p){return p._id == id;}); 
    }
    
    /*finding post in comment by inex*/
    $scope.findComment = function(postId, commentIndex){
        return findPost(postId).comments[commentIndex];
    }


    //helper methods
    $scope.scroll = function(anchor){
        $location.hash(anchor);
        $anchorScroll();
    }

    //init
//    $scope.listPosts();
    //cleanup method
    var resetPage = $scope.resetPage = function(){
        $scope.post = null;
        $scope.md = null;
        $scope.res = null;
    }

    $scope.setUser = function(flag){
        console.log('add user: ' + flag);
        if(flag){
            auth.setUser({name: 'dummy user', email:'sameh.hussein@gmail.com', roles:['test', 'user', 'admin']});
        }else{
            auth.setUser(null);
        }

    }

});



